package br.org.universa.sgos.util;

import java.math.BigInteger;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class HashUtil {

    private static HashUtil instancia;

    private HashUtil() {

    }

    public static HashUtil get() {

        if (instancia == null) {
            instancia = new HashUtil();
        }

        return instancia;
    }

    public String calcHash(String mensagem, String algoritmo) {

        MessageDigest digest;

        try {
            digest = MessageDigest.getInstance(algoritmo);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Algoritmo de hash nao suportado: " + algoritmo);
        }

        digest.update(mensagem.getBytes(), 0, mensagem.length());
        BigInteger i = new BigInteger(1, digest.digest());

        return String.format("%1$032x", i);
    }

}